﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Edc.Tfs2MantisAddin
{
    public partial class FormIssue : Form
    {
        public FormIssue()
        {
            InitializeComponent();

            this.txtTimeTracking.DataBindings.Add("Text", this, "TimeTracking", true, DataSourceUpdateMode.OnPropertyChanged);
        }


        public Edc.MMAssemblyManager.AssemblyMetadataManager AssemblyMetadataManager { get; set; }

        public Edc.MantisClient.Mantis Mantis { get; set; }

        public Edc.MantisClient.Issue Issue { get; set; }

        public TimeSpan TimeTracking { get; set; }

        private void FormIssue_Load(object sender, EventArgs e)
        {
            this.cmbIssueId.DataSource = AssemblyMetadataManager.GetPendingIssueIds();
            this.Mantis.Working += new EventHandler(Mantis_Working);
            this.Mantis.Done += new EventHandler(Mantis_Done);
        }

        void Mantis_Working(object sender, EventArgs e)
        {
            this.Enabled = false;
        }

        void Mantis_Done(object sender, EventArgs e)
        {
            this.Enabled = true;
        }

        private void cmbIssueId_SelectedValueChanged(object sender, EventArgs e)
        {
            Edc.MantisClient.Issue issue = null;
            if (cmbIssueId.SelectedValue != null)
            {
                issue = this.Mantis.GetIssue((int)cmbIssueId.SelectedValue, true);
            }

            this.Issue = issue;

            if (issue == null)
            {
                this.txtIssueSummary.Text = "";
                lvAssembly.DataSource = new List<Edc.MantisClient.Assembly>();
                btnOk.Enabled = false;
                btnDelete.Enabled = false;
                return;
            }

            // Gestione scripts. Ok, non andrebbe qui, ma non al momento non saprei dove!
            foreach (var script in AssemblyMetadataManager.GetIssueSqlScripts(issue.Id))
            {
                issue.AddFile(script);
                var assembly = new Edc.MMAssemblyManager.AssemblyMetadata() { AssemblyName = Path.GetFileName(script), IssueId = issue.Id };
                this.AssemblyMetadataManager.SaveMetadata(assembly);
            }

            this.txtIssueSummary.Text = this.Issue.Summary;
            lvAssembly.DataSource = this.AssemblyMetadataManager.GetIssueAssemblyMetadata(issue.Id);
            btnOk.Enabled = true;
            btnDelete.Enabled = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Vuoi eliminare i dati degli assembly pendenti per la issue " + this.Issue.Id + "?",
                "Conferma eliminazione dati", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK) return;

            AssemblyMetadataManager.DeleteIssueAssemblyMetadata(this.Issue.Id);
            this.cmbIssueId.DataSource = AssemblyMetadataManager.GetPendingIssueIds();
            if (this.cmbIssueId.Items.Count > 0)
            {
                this.cmbIssueId.SelectedIndex = 0;
            }
            else
            {
                this.cmbIssueId.SelectedIndex = -1;
                // Qui ci vorrebbe un BindingSource, lo so!
                this.Issue = null;
                cmbIssueId_SelectedValueChanged(cmbIssueId, EventArgs.Empty);
            }
        }

        private void FormIssue_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.DialogResult != DialogResult.OK) return;

            // Risalviamo i metadati, perché la griglia ci pemette di editarli.
            foreach(var assembly in (IList<Edc.MMAssemblyManager.AssemblyMetadata>) lvAssembly.DataSource)
            {
                AssemblyMetadataManager.SaveMetadata(assembly);
            }
        }
    }
}
